// import {
//   login,
//   getInfo
// } from '@/api/login'
import {
  login,
  getInfo
} from '@/api/login1'
import api from "@/api/login1.js";
import {
  getToken,
  setToken,
  removeToken
} from '@/utils/auth'
import {
  getMenuByUser
} from '../../api/login'
console.log()
const user = {
  state: {
    token: getToken(),
    user: {},
    id: '',
    roles: [],
    permission_menus: [],
    current_center: {},
    current_menus: [],
    check_menus: [],
    // 第一次加载菜单时用到
    loadMenus: false
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_USER: (state, user) => {
      state.user = user
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_LOAD_MENUS: (state, loadMenus) => {
      state.loadMenus = loadMenus
    },
    SET_PERMISSION_MENUS: (state, permission_menus) => {
      state.permission_menus = permission_menus
    },
    SET_CURRENT_MENUS: (state, current_menus) => {
      state.current_menus = current_menus
    },
    SET_CHECK_MENUS: (state, check_menus) => {
      state.check_menus = check_menus
    },
    SET_CURRENT_CENTER: (state, current_center) => {
      state.current_center = current_center
    }
  },

  actions: {
    // 登录
    Login({
      commit
    }, userInfo) {
      const {
        rememberMe,
        loginName,
        password
      } = userInfo
      return new Promise((resolve, reject) => {
     
        api.login("/data/index").then(aa => {
          console.log(res)
          // alert(1)
          // return
          let res={
            code:0,
            data:{
              sessionId:123123,
              userId:233
            }
          }
          if (res.code == 0) {
            // const {
            //   cpd_session_id
            // } = res.data
            // setToken(cpd_session_id, rememberMe)
            console.log(res)
            const cpd_session_id = res.data.sessionId

            localStorage.setItem('userId', res.data.userId)
            // user.state.id = res.data.adminManagerVO.id
            setToken(cpd_session_id, rememberMe)
            commit('SET_TOKEN', cpd_session_id)
            commit('SET_USER', res.data)
            // setUserInfo(res.data, commit)
            // 第一次加载菜单时用到， 具体见 src 目录下的 permission.js
            // commit('SET_LOAD_MENUS', true)
            // alert(2)
            resolve()
          } else {
            resolve()
          }
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({
      commit
    }) {
      return new Promise((resolve, reject) => {
        // console.log(this.state, 'asda')
        // alert()
        const params = {
          userId: user.state.id
        }
   
        api.getInfo('/data/index').then(aaa => {
        
          const arr1 = [
          //  1.用户管理
            { id: 100, pid: 0, name: '用户管理', value: '', icon: 'user', type: 0, uri: '/customer', status: 1, createTime: 1580807095000, sort: 1 },
            { id: 110, pid: 100, name: '用户列表', value: 'customer', icon: '', type: 1, uri: 'list', status: 1, createTime: 1538211117000, sort: 1 },
            { id: 111, pid: 110, name: '查询用户详情', value: 'customer:view', icon: null, type: 2, uri: '', status: 1, createTime: 1538209387000, sort: 999 },
            { id: 113, pid: 110, name: '充值', value: 'customer:recharge', icon: null, type: 2, uri: '', status: 1, createTime: 1538209387000, sort: 999 },
            { id: 112, pid: 110, name: '导出', value: 'customer:export', icon: null, type: 2, uri: '', status: 1, createTime: 1538209387000, sort: 999 },
            //    2.记录管理
            //  { id: 200, pid: 0, name: '资金管理', value: '', icon: 'user', type: 0, uri: '/money', status: 1, createTime: 1580807095000, sort: 2 },
            { id: 200, pid: 0, name: '资金管理', value: '', icon: 'user', type: 0, uri: '/records', status: 1, createTime: 1580807095000, sort: 2 },
            { id: 210, pid: 200, name: '资金列表', value: 'recordsorder', icon: '', type: 1, uri: 'order', status: 1, createTime: 1538211117000, sort: 1 },
            { id: 220, pid: 220, name: '资金流水', value: 'recordsbenefits', icon: '', type: 1, uri: 'benefits', status: 1, createTime: 1538211117000, sort: 999 },
            //    3.数据管理
            // { id: 300, pid: 0, name: '数据管理', value: '', icon: 'user', type: 0, uri: '/data', status: 1, createTime: 1580807095000, sort: 3 },
            // { id: 210, pid: 300, name: '挖矿收益配置', value: 'datadig', icon: '', type: 1, uri: 'dig', status: 1, createTime: 1538211117000, sort: 1 },
            // { id: 220, pid: 300, name: '分享收益配置', value: 'datashare', icon: '', type: 1, uri: 'share', status: 1, createTime: 1538211117000, sort: 2 },
            // { id: 230, pid: 300, name: '矿池收益配置', value: 'datapool', icon: '', type: 1, uri: 'pool', status: 1, createTime: 1538211117000, sort: 3 },

            //     4.竞技赛管理
            // { id: 400, pid: 0, name: '竞技赛管理', value: '', icon: 'user', type: 0, uri: '/event', status: 1, createTime: 1580807095000, sort: 4 },
            // { id: 410, pid: 400, name: '竞技赛列表', value: 'eventlist', icon: '', type: 1, uri: 'list', status: 1, createTime: 1538211117000, sort: 3 },

            // { id: 410, pid: 400, name: '竞技赛列表', value: 'eventlist', icon: 'user', type: 1, uri: 'eventlist', status: 1, createTime: 1580807095000, sort: 4 },

            // 5.平台配置
            { id: 500, pid: 0, name: '运营管理', value: '', icon: 'user', type: 0, uri: '/placfg', status: 1, createTime: 1580807095000, sort: 5 },
            { id: 510, pid: 500, name: '轮播管理', value: 'placfgnotice', icon: null, type: 1, uri: 'notice', status: null, createTime: 1595212868000, sort: 1 },
            { id: 520, pid: 500, name: '公告管理', value: 'placfgposters', icon: null, type: 1, uri: 'posters', status: null, createTime: 1595212868000, sort: 2 },
            { id: 530, pid: 500, name: '平台配置', value: 'placfgaper', icon: null, type: 1, uri: 'aper', status: null, createTime: 1595212868000, sort: 3 },
            { id: 540, pid: 500, name: ' 软件包列表', value: 'placfgairplane', icon: null, type: 1, uri: 'airplane', status: null, createTime: 1595212868000, sort: 4 },
            // { id: 550, pid: 500, name: '邮箱', value: 'placfgemail', icon: null, type: 1, uri: 'email', status: null, createTime: 1595212868000, sort: 5 },

            // 6.权限管理
            { id: 900, pid: 0, name: '权限', value: '', icon: 'zujian', type: 0, uri: '/permission', status: 1, createTime: 1594180701000, sort: 10 },
            { id: 910, pid: 900, name: '成员管理', value: 'permissionUser', icon: '', type: 1, uri: 'user', status: 1, createTime: 1594180746000, sort: 1 },

            { id: 911, pid: 910, name: '添加成员', value: 'user:add', icon: null, type: 2, uri: null, status: 1, createTime: 1594182756000, sort: 999 },
            { id: 912, pid: 910, name: '编辑成员', value: 'user:edit', icon: null, type: 2, uri: null, status: 1, createTime: 1594182802000, sort: 999 },
            { id: 913, pid: 910, name: '删除成员', value: 'user:del', icon: null, type: 2, uri: null, status: 1, createTime: 1594182828000, sort: 999 },
            { id: 920, pid: 900, name: '角色管理', value: 'permissionRole', icon: null, type: 1, uri: 'role', status: 1, createTime: 1594189356000, sort: 2 },
            { id: 921, pid: 920, name: '添加角色', value: 'role:add', icon: null, type: 2, uri: null, status: null, createTime: 1594190164000, sort: 999 },
            { id: 922, pid: 920, name: '编辑角色', value: 'role:edit', icon: null, type: 2, uri: null, status: null, createTime: 1594190177000, sort: 999 }
            // { id: 923, pid: 920, name: "删除角色
          ]
          // return
          // const arr1 = [
          //   // 用户管理
          //   { id: 100, pid: 0, name: "用户管理", value: "", icon: "user", type: 0, uri: "/customer", status: 1, createTime: 1580807095000, sort: 1},
          //   { id: 110, pid: 100, name: "用户列表", value: "customer", icon: "", type: 1, uri: "list", status: 1, createTime: 1538211117000, sort: 1},
          //   { id: 111, pid: 110, name: "查询用户详情", value: "customer:view", icon: null, type: 2, uri: "", status: 1, createTime: 1538209387000, sort: 999},
          //   { id: 113, pid: 110, name: "充值", value: "customer:recharge", icon: null, type: 2, uri: "", status: 1, createTime: 1538209387000, sort: 999},
          //  2{ id: 112, pid: 110, name: "导出", value: "customer:export", icon: null, type: 2, uri: "", status: 1, createTime: 1538209387000, sort: 999},

          //   // 资金管理
          //   { id: 200, pid: 0, name: '资金管理', value: '', icon: 'user', type: 1, uri: '/money', status: 1, createTime: 1580807095000, sort: 2 },
          //   { id: 220, pid: 200, name: '充值记录', value: 'moneyTopup', icon: '', type: 1, uri: 'topup', status: 1, createTime: 1538211117000, sort: 1 },
          //  1{ id: 221, pid: 220, name: "导出", value: "topup:export", icon: null, type: 2, uri: "", status: 1, createTime: 1538209387000, sort: 999},
          //   { id: 230, pid: 200, name: '提现记录', value: 'moneyWithdrawal', icon: '', type: 1, uri: 'withdrawal', status: 1, createTime: 1538211117000, sort: 2 },
          //  2{ id: 231, pid: 230, name: "导出", value: "withdrawal:export", icon: null, type: 2, uri: "", status: 1, createTime: 1538209387000, sort: 999},

          //   { id: 240, pid: 200, name: '内部转账记录', value: 'moneyTransfer', icon: '', type: 1, uri: 'transfer', status: 1, createTime: 1538211117000, sort: 3 },
          //  3{ id: 241, pid: 240, name: "导出", value: "transfer:export", icon: null, type: 2, uri: "", status: 1, createTime: 1538209387000, sort: 999},

          //   { id: 250, pid: 200, name: 'BOK转出记录', value: 'moneyRollout', icon: '', type: 1, uri: 'rollout', status: 1, createTime: 1538211117000, sort: 4 },
          //  4{ id: 251, pid: 250, name: "导出", value: "rollout:export", icon: null, type: 2, uri: "", status: 1, createTime: 1538209387000, sort: 999},

          //   { id: 260, pid: 200, name: '参与节点记录', value: 'moneyNode', icon: '', type: 1, uri: 'node', status: 1, createTime: 1538211117000, sort: 5 },
          //  5{ id: 261, pid: 260, name: "导出", value: "node:export", icon: null, type: 2, uri: "", status: 1, createTime: 1538209387000, sort: 999},

          //   { id: 270, pid: 200, name: '平台充币记录', value: 'moneyCharge', icon: '', type: 1, uri: 'Charge', status: 1, createTime: 1538211117000, sort: 6 },
          //  6{ id: 271, pid: 270, name: "导出", value: "Charge:export", icon: null, type: 2, uri: "", status: 1, createTime: 1538209387000, sort: 999},

          //   // 平台管理
          //   { id: 300, pid: 0, name: '平台管理', value: '', icon: 'user', type: 1, uri: '/platform',  status: 1, createTime: 1580807095000, sort: 3 },
          //   { id: 310, pid: 300, name: '节点金额配置', value: 'platformNodeamount', icon: '', type: 1, uri: 'nodeamount', status: 1, createTime: 1538211117000, sort: 1 },
          //   { id: 320, pid: 300, name: '分享收益配置', value: 'platformShare', icon: '', type: 1, uri: 'share', status: 1, createTime: 1538211117000, sort: 2 },
          //   { id: 330, pid: 300, name: '推荐收益配置', value: 'platformRecommended', icon: '', type: 1, uri: 'recommended', status: 1, createTime: 1538211117000, sort: 3 },
          //   { id: 340, pid: 300, name: '团队收益配置', value: 'platformTeam', icon: '', type: 1,  uri: 'team', status: 1, createTime: 1538211117000, sort: 4 },
          //   { id: 350, pid: 300, name: '团队等级配置', value: 'platformTeamlevel', icon: '', type: 1, uri: 'teamlevel', status: 1, createTime: 1538211117000, sort: 5 },
          //   { id: 360, pid: 300, name: '转出手续费配置', value: 'platformPoundage', icon: '', type: 1, uri: 'poundage', status: 1, createTime: 1538211117000, sort: 6 },
          //   // 超环博弈
          //   { id: 400, pid: 0, name: '超环博弈管理', value: '', icon: 'user', type: 1, uri: '/game',  status: 1, createTime: 1580807095000, sort: 4 },
          //   { id: 410, pid: 400, name: '虚拟盘', value: 'gameVirtual', icon: '', type: 1, uri: 'virtual', status: 1, createTime: 1538211117000, sort: 1 },
          //   // 软件包管理
          //   { id: 500, pid: 0, name: "软件包管理", value: "", icon: "app", type: 0, uri: "software", status: 1, createTime: 1608000248000, sort: 5},
          //   { id: 510, pid: 500, name: "软件包列表", value: "softwarelist", icon: null, type: 1, uri: "list", status: 1, createTime: 1608000730000, sort: 1},
          //   { id: 511, pid: 510, name: "软件包编辑", value: "software:edit", icon: null, type: 2, uri: null, status: 1, createTime: 1608003717000, sort: 999},
          //   // 币币系统
          //   { id: 600, pid: 0, name: "币币系统", value: "", icon: "database", type: 0, uri: "currcoin", status: 1, createTime: 1608084196000, sort: 6},
          //   { id: 610, pid: 600, name: "订单列表", value: "currcoinorder", icon: null, type: 1, uri: "orderslist", status: 1, createTime: 1609146605000, sort: 1},
          //   // 运营管理
          //   { id: 700, pid: 0, name: "运营管理", value: null, icon: "education", type: 0, uri: "/operation", status: null, createTime: 1595212845000, sort: 7},
          //   { id: 710, pid: 700, name: "公告管理", value: "notice", icon: null, type: 1, uri: "notice", status: null, createTime: 1595212868000, sort: 1},
          //   { id: 711, pid: 710, name: "新增公告", value: "notice:add", icon: null, type: 2, uri: null, status: null, createTime: 1597112329000, sort: 999},
          //   { id: 712, pid: 710, name: "删除公告", value: "notice:del", icon: null, type: 2, uri: null, status: null, createTime: 1597112365000, sort: 999},
          //   { id: 713, pid: 710, name: "编辑公告", value: "notice:edit", icon: null, type: 2, uri: null, status: null, createTime: 1597408637000, sort: 999},
          //   // 权限管理
          //   { id: 900, pid: 0, name: "权限", value: "", icon: "zujian", type: 0, uri: "/permission", status: 1, createTime: 1594180701000, sort: 10},
          //   { id: 910, pid: 900, name: "成员管理", value: "permissionUser", icon: "", type: 1, uri: "user", status: 1, createTime: 1594180746000, sort: 1},
          //   { id: 911, pid: 910, name: "添加成员", value: "user:add", icon: null, type: 2, uri: null, status: 1, createTime: 1594182756000, sort: 999},
          //   { id: 912, pid: 910, name: "编辑成员", value: "user:edit", icon: null, type: 2, uri: null, status: 1, createTime: 1594182802000, sort: 999},
          //   { id: 913, pid: 910, name: "删除成员", value: "user:del", icon: null, type: 2, uri: null, status: 1, createTime: 1594182828000, sort: 999},
          //   { id: 920, pid: 900, name: "角色管理", value: "permissionRole", icon: null, type: 1, uri: "role", status: 1, createTime: 1594189356000, sort: 2},
          //   { id: 921, pid: 920, name: "添加角色", value: "role:add", icon: null, type: 2, uri: null, status: null, createTime: 1594190164000, sort: 999},
          //   { id: 922, pid: 920, name: "编辑角色", value: "role:edit", icon: null, type: 2, uri: null, status: null, createTime: 1594190177000, sort: 999},
          //   { id: 923, pid: 920, name: "删除角色", value: "role:del", icon: null, type: 2, uri: null, status: null, createTime: 1594190194000, sort: 999},
          // ]
          // alert(res.data.privilegeList)
          // console.log(res)
          let res={
            data:[]
          }
          console.log(res)
         
           res.data.privilegeList=[]
          const arr = []
          // return;
          const resource = arr.concat(arr1)
       
          const menus = resource.filter(item => {
            return item && item.type !== 2
          })
          setUserInfo({
            ...res.data,
            menus
          }, commit)
          resolve({
            ...res.data,
            menus
          })
        }).catch(error => {
          // alert(error)

          reject(error)
        })
      })
    },
    // 登出
    LogOut({
      commit
    }) {
      return new Promise((resolve, reject) => {
        logOut(commit)
        resolve()
        // logout().then(res => {
        //
        // }).catch(error => {
        //   logOut(commit)
        //   reject(error)
        // })
      })
    },

    updateLoadMenus({
      commit
    }) {
      return new Promise((resolve, reject) => {
        commit('SET_LOAD_MENUS', false)
      })
    }
  }
}

export const logOut = (commit) => {
  commit('SET_TOKEN', '')
  commit('SET_ROLES', [])
  removeToken()
}

function toTree(data) {
  // 删除 所有 children,以防止多次调用
  data.forEach(function(item) {
    delete item.children
  })

  // 将数据存储为 以 id 为 KEY 的 map 索引数据列
  const map = {}
  data.forEach(function(item) {
    map[item.id] = item
  })
  //        console.log(map);
  const val = []
  data.forEach(function(item) {
    // 以当前遍历项，的pid,去map对象中找到索引的id
    const parent = map[item.pid]
    // 好绕啊，如果找到索引，那么说明此项不在顶级当中,那么需要把此项添加到，他对应的父级中
    if (parent) {
      (parent.children || (parent.children = [])).push(item)
    } else {
      // 如果没有在map中找到对应的索引ID,那么直接把 当前的item添加到 val结果集中，作为顶级
      val.push(item)
    }
  })
  return val
}

export const setUserInfo = (res, commit) => {
  // 如果没有任何权限，则赋予一个默认的权限，避免请求死循环
  const {
    privilegeList,
    menus
  } = res
  const roles = (privilegeList || []).map(element => {
    return element && element.value
  })
  menus.sort((a, b) => {
    return a.sort - b.sort
  })
  commit('SET_PERMISSION_MENUS', toTree(menus))
  if (roles.length === 0) {
    commit('SET_ROLES', ['ROLE_SYSTEM_DEFAULT'])
  } else {
    commit('SET_ROLES', roles)
  }
  commit('SET_USER', res)
}

export default user
